import 'package:flutter/material.dart';

import 'package:charts_flutter/flutter.dart' as charts;
import 'package:drm/base/tj_date_entity.dart';
import 'two_entity.dart';
import 'package:drm/util/utils.dart';

class TwoPage extends StatefulWidget {
  List<TwoEntity> data;

  TwoPage({Key key, this.data}) : super(key: key);

  @override
  _TwoPageState createState() => _TwoPageState();
}

class _TwoPageState extends State<TwoPage> {

  @override
  Widget build(BuildContext context) {
    return charts.BarChart(
      _createSampleData(),
      animate: true,
      vertical: false,
      barRendererDecorator: new charts.BarLabelDecorator<String>(),
      domainAxis:
      new charts.OrdinalAxisSpec(renderSpec: new charts.SmallTickRendererSpec(
//        labelStyle: charts.TextStyleSpec(fontSize: 10),

      )),
      barGroupingType: charts.BarGroupingType.grouped,
      behaviors: [new charts.SeriesLegend(
        position: charts.BehaviorPosition.top,

      )],


    );
  }

//  String hHSL;
//  String tHSL;
//  String dHZS;

  charts.Series<TjDateEntity, String> iniRow(String title,List<TjDateEntity> data,charts.Color color) {
    return charts.Series<TjDateEntity, String>(
      id: title,
      domainFn: (TjDateEntity sales, _)=>MyUtils.getChartLeftTitle(sales.name),
      measureFn: (TjDateEntity sales, _) {
        try {
          if (sales.value.isEmpty) {
            return 0 ;
          }else {
            return double.parse(sales.value);
          }

        } catch (e) {
          return 0;
        }
      },
      data: data,
      labelAccessorFn: (TjDateEntity sales, _) => '${sales.value.isEmpty?'0':sales.value}',
      fillColorFn: (_, __) =>color,
      colorFn: (TjDateEntity sales, _) =>color,
    );
  }


  List<charts.Series<TjDateEntity, String>> _createSampleData() {
    List<TjDateEntity> hhslData = <TjDateEntity>[];
    List<TjDateEntity> thslData = <TjDateEntity>[];
    List<TjDateEntity> dhszData = <TjDateEntity>[];
    List<TwoEntity> datas = widget.data;
    datas.forEach((value) {
      hhslData.add(TjDateEntity(name: value.name, value: value.hHSL));
      thslData.add(TjDateEntity(name: value.name, value: value.tHSL));
      dhszData.add(TjDateEntity(name: value.name, value: value.dHZS));
    });

    return [
      iniRow('订货',dhszData,charts.MaterialPalette.blue.shadeDefault),
      iniRow('换货',hhslData,charts.MaterialPalette.deepOrange.shadeDefault),
      iniRow('退货',thslData,charts.MaterialPalette.green.shadeDefault),

    ];
  }
}
